package de.dhbw.ka.tit09.picsim.ui;

import java.io.File;
import java.io.IOException;

import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.action.StatusLineManager;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ColumnPixelData;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.ResourceManager;
import org.eclipse.wb.swt.SWTResourceManager;

import de.dhbw.ka.tit09.picsim.RealmProvider;
import de.dhbw.ka.tit09.picsim.ui.controller.MainWindowController;

/**
 * Main class for the simulator and the GUI.
 * 
 * @author Andreas Bender
 * 
 */
public class MainWindow extends ApplicationWindow {
	private Action actionExit;
	private Action actionOpenFile;
	private Action actionOpenDocu;

	private MainWindowController controller;

	private Combo comboCOMPort;
	private Button btHardwareEnable;

	private TableViewer sourceTextTableViewer;
	private TableViewer stackTableViewer;
	private TableViewer generalPurposeTableViewer;
	private TableViewer eEPROMTableViewer;
	private Table table_1;
	private Text tTMR0;
	private Text tOPTION;
	private Text tPCL;
	private Text tSTATUS;
	private Text tFSR;
	private Text tPORTA;
	private Text tPORTB;
	private Text tTRISA;
	private Text tTRISB;
	private Text tEEDATA;
	private Text tEEADR;
	private Text tEECON1;
	private Text tPCLATH;
	private Text tINTCON;
	private Table table;
	private Action actionRun;
	private Action actionWatch;
	private Action actionStop;
	private Action actionPause;
	private Table table_2;
	private Table table_3;
	private Label lblRuntimeValue;

	/**
	 * Returns the runtime label value.
	 * 
	 * @return the runtime label value
	 */
	public Label getLblRuntimeValue() {
		return lblRuntimeValue;
	}

	/**
	 * Returns the source text table viewer.
	 * 
	 * @return the source text table viewer
	 */
	public TableViewer getSourceTextTableViewer() {
		return sourceTextTableViewer;
	}

	/**
	 * Returns the stack table viewer.
	 * 
	 * @return the stack table viewer
	 */
	public TableViewer getStackTableViewer() {
		return stackTableViewer;
	}

	/**
	 * Returns the general purpose registers table viewer
	 * 
	 * @return the GP registers table viewer
	 */
	public TableViewer getGeneralPurposeTableViewer() {
		return generalPurposeTableViewer;
	}

	/**
	 * Returns the EEPROM registers table viewer.
	 * 
	 * @return the EEPROM table viewer
	 */
	public TableViewer getEEPROMTableViewer() {
		return eEPROMTableViewer;
	}

	/**
	 * Create the application window.
	 */
	public MainWindow() {
		super(null);
		createActions();
		addToolBar(SWT.FLAT | SWT.WRAP);
		addMenuBar();
		addStatusLine();
	}

	/**
	 * Create contents of the application window.
	 * 
	 * @param parent
	 */
	@Override
	protected Control createContents(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		container.setLayout(new GridLayout(3, false));

		Composite composite_3 = new Composite(container, SWT.NONE);
		GridData gridData = new GridData();
		gridData.verticalAlignment = GridData.FILL;
		// gridData.verticalSpan = 2;
		gridData.grabExcessVerticalSpace = true;
		composite_3.setLayoutData(gridData);
		composite_3.setLayout(new FillLayout(SWT.HORIZONTAL));

		Composite composite = new Composite(composite_3, SWT.BORDER);
		TableColumnLayout tcl_composite = new TableColumnLayout();
		composite.setLayout(tcl_composite);
		sourceTextTableViewer = new TableViewer(composite, SWT.FULL_SELECTION);
		sourceTextTableViewer
				.setContentProvider(new ObservableListContentProvider());
		table_1 = sourceTextTableViewer.getTable();
		table_1.setFont(SWTResourceManager
				.getFont("Courier New", 9, SWT.NORMAL));
		table_1.setHeaderVisible(true);

		TableViewerColumn tableViewerBPColumn = new TableViewerColumn(
				sourceTextTableViewer, SWT.NONE);
		tableViewerBPColumn.setLabelProvider(new BreakpointColumnLabelProvider(
				BeansObservables.observeMaps(
						((ObservableListContentProvider) sourceTextTableViewer
								.getContentProvider()).getKnownElements(),
						new String[] { "command", "checked" })));
		tableViewerBPColumn.setEditingSupport(new BreakpointEditingSupport(
				sourceTextTableViewer));
		TableColumn breakpointColumn = tableViewerBPColumn.getColumn();
		breakpointColumn.setResizable(false);
		tcl_composite.setColumnData(breakpointColumn, new ColumnPixelData(25,
				false, true));
		breakpointColumn.setText("BP");

		TableViewerColumn tableViewerSCColumn = new TableViewerColumn(
				sourceTextTableViewer, SWT.NONE);
		tableViewerSCColumn.setLabelProvider(new SourceTextColumnLabelProvider(
				BeansObservables.observeMaps(
						((ObservableListContentProvider) sourceTextTableViewer
								.getContentProvider()).getKnownElements(),
						new String[] { "highlighted", "text" })));
		TableColumn sourceCodeColumn = tableViewerSCColumn.getColumn();
		tcl_composite.setColumnData(sourceCodeColumn, new ColumnPixelData(510,
				false, true));
		sourceCodeColumn.setResizable(false);
		sourceCodeColumn.setText("Source Code");

		Composite composite_1 = new Composite(container, SWT.NONE);
		composite_1.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false,
				1, 1));
		GridLayout gl_composite_1 = new GridLayout(7, false);
		gl_composite_1.verticalSpacing = 6;
		gl_composite_1.marginLeft = 20;
		composite_1.setLayout(gl_composite_1);

		VerifyListener sfrRegisterVerifyListener = new VerifyListener() {
			public void verifyText(VerifyEvent event) {
				event.doit = false;

				String text = event.text.toUpperCase();

				// allow hex values, backspace and delete key
				if (text.matches("[0-9A-F]+") || event.character == SWT.BS
						|| event.character == SWT.DEL) {
					event.doit = true;
				}

				event.text = text;
			}
		};

		Label lblTmr0 = new Label(composite_1, SWT.NONE);
		lblTmr0.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		lblTmr0.setText("TMR0:");

		tTMR0 = new Text(composite_1, SWT.BORDER);
		tTMR0.setTextLimit(2);
		tTMR0.setLayoutData(new GridData(15, 15));
		tTMR0.addVerifyListener(sfrRegisterVerifyListener);
		new Label(composite_1, SWT.NONE);

		Label lblEedata = new Label(composite_1, SWT.NONE);
		lblEedata.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblEedata.setText("EEDATA:");

		tEEDATA = new Text(composite_1, SWT.BORDER);
		tEEDATA.setTextLimit(2);
		tEEDATA.setLayoutData(new GridData(15, 15));
		tEEDATA.addVerifyListener(sfrRegisterVerifyListener);
		new Label(composite_1, SWT.NONE);

		stackTableViewer = new TableViewer(composite_1, SWT.BORDER
				| SWT.FULL_SELECTION);
		stackTableViewer
				.setContentProvider(new ObservableListContentProvider());
		table = stackTableViewer.getTable();
		table.setLinesVisible(true);
		GridData gd_table = new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 7);
		gd_table.widthHint = 23;
		gd_table.horizontalIndent = 10;
		// gd_table.heightHint = 160;
		table.setLayoutData(gd_table);
		table.setHeaderVisible(true);

		TableViewerColumn tableViewerColumn_1 = new TableViewerColumn(
				stackTableViewer, SWT.NONE);
		tableViewerColumn_1.setLabelProvider(new ColumnLabelProvider());
		TableColumn tblclmnValue = tableViewerColumn_1.getColumn();
		tblclmnValue.setWidth(40);
		tblclmnValue.setText("Stack");

		Label lblOption = new Label(composite_1, SWT.NONE);
		lblOption.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblOption.setText("OPTION:");

		tOPTION = new Text(composite_1, SWT.BORDER);
		tOPTION.setTextLimit(2);
		tOPTION.setLayoutData(new GridData(15, 15));
		tOPTION.addVerifyListener(sfrRegisterVerifyListener);
		new Label(composite_1, SWT.NONE);

		Label lblEeadr = new Label(composite_1, SWT.NONE);
		lblEeadr.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblEeadr.setText("EEADR:");

		tEEADR = new Text(composite_1, SWT.BORDER);
		tEEADR.setTextLimit(2);
		tEEADR.setLayoutData(new GridData(15, 15));
		tEEADR.addVerifyListener(sfrRegisterVerifyListener);
		new Label(composite_1, SWT.NONE);

		Label lblPcl = new Label(composite_1, SWT.NONE);
		lblPcl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		lblPcl.setText("PCL:");

		tPCL = new Text(composite_1, SWT.BORDER);
		tPCL.setTextLimit(2);
		tPCL.setLayoutData(new GridData(15, 15));
		tPCL.addVerifyListener(sfrRegisterVerifyListener);
		new Label(composite_1, SWT.NONE);

		Label lblEecon1 = new Label(composite_1, SWT.NONE);
		lblEecon1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblEecon1.setText("EECON1:");

		tEECON1 = new Text(composite_1, SWT.BORDER);
		tEECON1.setTextLimit(2);
		tEECON1.setLayoutData(new GridData(15, 15));
		tEECON1.addVerifyListener(sfrRegisterVerifyListener);
		new Label(composite_1, SWT.NONE);

		Label lblStatus = new Label(composite_1, SWT.NONE);
		lblStatus.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblStatus.setText("STATUS:");

		tSTATUS = new Text(composite_1, SWT.BORDER);
		tSTATUS.setTextLimit(2);
		tSTATUS.setLayoutData(new GridData(15, 15));
		tSTATUS.addVerifyListener(sfrRegisterVerifyListener);
		new Label(composite_1, SWT.NONE);

		Label lblPclath = new Label(composite_1, SWT.NONE);
		lblPclath.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblPclath.setText("PCLATH:");

		tPCLATH = new Text(composite_1, SWT.BORDER);
		tPCLATH.setTextLimit(2);
		tPCLATH.setLayoutData(new GridData(15, 15));
		tPCLATH.addVerifyListener(sfrRegisterVerifyListener);
		new Label(composite_1, SWT.NONE);

		Label lblFsr = new Label(composite_1, SWT.NONE);
		lblFsr.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		lblFsr.setText("FSR:");

		tFSR = new Text(composite_1, SWT.BORDER);
		tFSR.setTextLimit(2);
		tFSR.setLayoutData(new GridData(15, 15));
		tFSR.addVerifyListener(sfrRegisterVerifyListener);
		new Label(composite_1, SWT.NONE);

		Label lblIntcon = new Label(composite_1, SWT.NONE);
		lblIntcon.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblIntcon.setText("INTCON:");

		tINTCON = new Text(composite_1, SWT.BORDER);
		tINTCON.setTextLimit(2);
		tINTCON.setLayoutData(new GridData(15, 15));
		tINTCON.addVerifyListener(sfrRegisterVerifyListener);
		new Label(composite_1, SWT.NONE);

		Label lblPorta = new Label(composite_1, SWT.NONE);
		lblPorta.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblPorta.setText("PORTA:");

		tPORTA = new Text(composite_1, SWT.BORDER);
		tPORTA.setTextLimit(2);
		tPORTA.setLayoutData(new GridData(15, 15));
		tPORTA.addVerifyListener(sfrRegisterVerifyListener);
		new Label(composite_1, SWT.NONE);

		Label lblTrisa = new Label(composite_1, SWT.NONE);
		lblTrisa.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblTrisa.setText("TRISA:");

		tTRISA = new Text(composite_1, SWT.BORDER);
		tTRISA.setTextLimit(2);
		tTRISA.setLayoutData(new GridData(15, 15));
		tTRISA.addVerifyListener(sfrRegisterVerifyListener);
		new Label(composite_1, SWT.NONE);

		Label lblPortb = new Label(composite_1, SWT.NONE);
		lblPortb.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblPortb.setText("PORTB:");

		tPORTB = new Text(composite_1, SWT.BORDER);
		tPORTB.setTextLimit(2);
		tPORTB.setLayoutData(new GridData(15, 15));
		tPORTB.addVerifyListener(sfrRegisterVerifyListener);
		new Label(composite_1, SWT.NONE);

		Label lblTrisb = new Label(composite_1, SWT.NONE);
		lblTrisb.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblTrisb.setText("TRISB:");

		tTRISB = new Text(composite_1, SWT.BORDER);
		tTRISB.setTextLimit(2);
		tTRISB.setLayoutData(new GridData(15, 15));
		new Label(composite_1, SWT.NONE);
		// -------------------------------
		Group grpEEPROM = new Group(composite_1, SWT.NONE);
		grpEEPROM.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 7, 1));
		grpEEPROM.setText("EEPROM");
		grpEEPROM.setLayout(new GridLayout(2, false));

		Label lblRom1 = new Label(grpEEPROM, SWT.NONE);
		lblRom1.setText("00h..07h");

		eEPROMTableViewer = new TableViewer(grpEEPROM, SWT.BORDER
				| SWT.FULL_SELECTION);
		eEPROMTableViewer.setLabelProvider(new MemoryTableLabelProvider());
		eEPROMTableViewer
				.setContentProvider(new ObservableListContentProvider());
		table_3 = eEPROMTableViewer.getTable();
		GridData gd_table_3 = new GridData(SWT.LEFT, SWT.CENTER, false, false,
				1, 8);
		gd_table_3.widthHint = 223;// 191+32=223
		gd_table_3.heightHint = 135;
		table_3.setLayoutData(gd_table_3);
		table_3.setLinesVisible(true);

		TableColumn tblclmnNewColumn_6 = new TableColumn(table_3, SWT.NONE);
		tblclmnNewColumn_6.setResizable(false);
		tblclmnNewColumn_6.setWidth(30);
		tblclmnNewColumn_6.setText("New Column");

		TableColumn tblclmnNewColumn_7 = new TableColumn(table_3, SWT.NONE);
		tblclmnNewColumn_7.setResizable(false);
		tblclmnNewColumn_7.setWidth(30);
		tblclmnNewColumn_7.setText("New Column");

		TableColumn tblclmnNewColumn_8 = new TableColumn(table_3, SWT.NONE);
		tblclmnNewColumn_8.setResizable(false);
		tblclmnNewColumn_8.setWidth(30);
		tblclmnNewColumn_8.setText("New Column");

		TableColumn tblclmnNewColumn_9 = new TableColumn(table_3, SWT.NONE);
		tblclmnNewColumn_9.setResizable(false);
		tblclmnNewColumn_9.setWidth(30);
		tblclmnNewColumn_9.setText("New Column");

		TableColumn tblclmnNewColumn_10 = new TableColumn(table_3, SWT.NONE);
		tblclmnNewColumn_10.setResizable(false);
		tblclmnNewColumn_10.setWidth(30);
		tblclmnNewColumn_10.setText("New Column");

		TableColumn tblclmnNewColumn_11 = new TableColumn(table_3, SWT.NONE);
		tblclmnNewColumn_11.setResizable(false);
		tblclmnNewColumn_11.setWidth(30);
		tblclmnNewColumn_11.setText("New Column");

		TableColumn tblclmnNewColumn_12 = new TableColumn(table_3, SWT.NONE);
		tblclmnNewColumn_12.setResizable(false);
		tblclmnNewColumn_12.setWidth(30);
		tblclmnNewColumn_12.setText("New Column");

		TableColumn tblclmnNewColumn_13 = new TableColumn(table_3, SWT.NONE);
		tblclmnNewColumn_13.setResizable(false);
		tblclmnNewColumn_13.setWidth(30);
		tblclmnNewColumn_13.setText("New Column");

		Label lblRom2 = new Label(grpEEPROM, SWT.NONE);
		lblRom2.setText("08h..0Fh");

		Label lblRom3 = new Label(grpEEPROM, SWT.NONE);
		lblRom3.setText("10h..17h");

		Label lblRom4 = new Label(grpEEPROM, SWT.NONE);
		lblRom4.setText("18h..1Fh");

		Label lblRom5 = new Label(grpEEPROM, SWT.NONE);
		lblRom5.setText("20h..27h");

		Label lblRom6 = new Label(grpEEPROM, SWT.NONE);
		lblRom6.setText("28h..2Fh");

		Label lblRom7 = new Label(grpEEPROM, SWT.NONE);
		lblRom7.setText("30h..37h");

		Label lblRom8 = new Label(grpEEPROM, SWT.NONE);
		lblRom8.setText("38h..3Fh");
		tTRISB.addVerifyListener(sfrRegisterVerifyListener);

		Composite composite_2 = new Composite(container, SWT.NONE);
		composite_2.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false,
				1, 1));
		composite_2.setLayout(new FormLayout());

		Group grpTimers = new Group(composite_2, SWT.NONE);
		grpTimers.setText("Timer");
		grpTimers.setLayout(new GridLayout(2, false));
		FormData fd_grpTimers = new FormData();
		grpTimers.setLayoutData(fd_grpTimers);

		Group grpHardwareCommunication = new Group(composite_2, SWT.NONE);
		fd_grpTimers.right = new FormAttachment(grpHardwareCommunication, 0, SWT.RIGHT);
		fd_grpTimers.left = new FormAttachment(grpHardwareCommunication, 0, SWT.LEFT);
		FormData fd_grpHardwareCommunication = new FormData();
		fd_grpHardwareCommunication.right = new FormAttachment(100);
		fd_grpHardwareCommunication.left = new FormAttachment(0);
		fd_grpHardwareCommunication.top = new FormAttachment(0);
		fd_grpHardwareCommunication.bottom = new FormAttachment(0, 53);
		grpHardwareCommunication.setLayoutData(fd_grpHardwareCommunication);
		grpHardwareCommunication.setText("Hardware Communication");

		btHardwareEnable = new Button(grpHardwareCommunication, SWT.CHECK);
		btHardwareEnable.setBounds(10, 22, 55, 16);
		btHardwareEnable.setText("Enable");

		comboCOMPort = new Combo(grpHardwareCommunication, SWT.NONE);
		comboCOMPort.setBounds(126, 20, 91, 23);

		Label lblCom = new Label(grpHardwareCommunication, SWT.NONE);
		lblCom.setBounds(92, 23, 28, 15);
		lblCom.setText("COM");

		Group grpGeneralPurposeRegisters = new Group(composite_2, SWT.NONE);
		fd_grpTimers.top = new FormAttachment(grpGeneralPurposeRegisters, 6);

		Label lblRuntime = new Label(grpTimers, SWT.NONE);
		lblRuntime.setText("Runtime:");

		lblRuntimeValue = new Label(grpTimers, SWT.NONE);
		GridData gd_lblRuntimeValue = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_lblRuntimeValue.widthHint = 186;
		lblRuntimeValue.setLayoutData(gd_lblRuntimeValue);
		FormData fd_grpGeneralPurposeRegisters = new FormData();
		fd_grpGeneralPurposeRegisters.right = new FormAttachment(grpTimers, 0, SWT.RIGHT);
		fd_grpGeneralPurposeRegisters.left = new FormAttachment(0);
		fd_grpGeneralPurposeRegisters.top = new FormAttachment(grpHardwareCommunication, 6);
		grpGeneralPurposeRegisters.setLayoutData(fd_grpGeneralPurposeRegisters);
		grpGeneralPurposeRegisters.setText("General Purpose Registers");
		grpGeneralPurposeRegisters.setLayout(new GridLayout(2, false));

		Label lblc = new Label(grpGeneralPurposeRegisters, SWT.NONE);
		lblc.setText("0Ch..11h");

		generalPurposeTableViewer = new TableViewer(grpGeneralPurposeRegisters,
				SWT.BORDER | SWT.FULL_SELECTION);
		generalPurposeTableViewer
				.setLabelProvider(new MemoryTableLabelProvider());
		generalPurposeTableViewer
				.setContentProvider(new ObservableListContentProvider());
		table_2 = generalPurposeTableViewer.getTable();
		GridData gd_table_2 = new GridData(SWT.LEFT, SWT.CENTER, false, false,
				1, 6);
		gd_table_2.widthHint = 164;// 139
		gd_table_2.heightHint = 97;
		table_2.setLayoutData(gd_table_2);
		table_2.setLinesVisible(true);

		TableColumn tblclmnNewColumn = new TableColumn(table_2, SWT.NONE);
		tblclmnNewColumn.setResizable(false);
		tblclmnNewColumn.setWidth(30);
		tblclmnNewColumn.setText("New Column");

		TableColumn tblclmnNewColumn_1 = new TableColumn(table_2, SWT.NONE);
		tblclmnNewColumn_1.setResizable(false);
		tblclmnNewColumn_1.setWidth(30);
		tblclmnNewColumn_1.setText("New Column");

		TableColumn tblclmnNewColumn_2 = new TableColumn(table_2, SWT.NONE);
		tblclmnNewColumn_2.setResizable(false);
		tblclmnNewColumn_2.setWidth(30);
		tblclmnNewColumn_2.setText("New Column");

		TableColumn tblclmnNewColumn_3 = new TableColumn(table_2, SWT.NONE);
		tblclmnNewColumn_3.setResizable(false);
		tblclmnNewColumn_3.setWidth(30);
		tblclmnNewColumn_3.setText("New Column");

		TableColumn tblclmnNewColumn_4 = new TableColumn(table_2, SWT.NONE);
		tblclmnNewColumn_4.setResizable(false);
		tblclmnNewColumn_4.setWidth(30);
		tblclmnNewColumn_4.setText("New Column");

		TableColumn tblclmnNewColumn_5 = new TableColumn(table_2, SWT.NONE);
		tblclmnNewColumn_5.setResizable(false);
		tblclmnNewColumn_5.setWidth(30);
		tblclmnNewColumn_5.setText("New Column");

		Label lblhh = new Label(grpGeneralPurposeRegisters, SWT.NONE);
		lblhh.setText("12h..17h");

		Label lblhdh = new Label(grpGeneralPurposeRegisters, SWT.NONE);
		lblhdh.setText("18h..1Dh");

		Label lblehh = new Label(grpGeneralPurposeRegisters, SWT.NONE);
		lblehh.setText("1Eh..23h");

		Label lblhh_1 = new Label(grpGeneralPurposeRegisters, SWT.NONE);
		lblhh_1.setText("24h..29h");

		Label lblahfh = new Label(grpGeneralPurposeRegisters, SWT.NONE);
		lblahfh.setText("2Ah..2Fh");

		controller = new MainWindowController(this);// FIXME

		return container;
	}

	/**
	 * Create the actions.
	 */
	private void createActions() {
		// Create the actions
		{
			actionExit = new Action("Exit") {
				public void run() {
					close();
					System.exit(0);
				}
			};
			actionExit.setAccelerator(SWT.CTRL | 'W');
		}
		{
			actionOpenFile = new Action("Open File...") {
				public void run() {
					FileDialog fd = new FileDialog(getShell(), SWT.OPEN);
					fd.setText("Open File");
					// fd.setFilterPath("C:/");
					String[] filterExt = { "*.LST", "*.*" };
					fd.setFilterExtensions(filterExt);
					String selected = fd.open();
					if (selected != null) {
						controller.openFile(selected);
					}
				}
			};
			actionOpenFile.setImageDescriptor(ResourceManager
					.getImageDescriptor(MainWindow.class,
							"/de/dhbw/ka/tit09/picsim/ui/icons/open.png"));
			actionOpenFile.setAccelerator(SWT.CTRL | 'O');
		}
		{
			actionOpenDocu = new Action("Open Documentation") {
				public void run() {
					try {
						String path = "files" + File.separator + "help.pdf";
						Runtime.getRuntime().exec("cmd /c " + path);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			};
			actionOpenDocu.setAccelerator(SWT.F1);
		}
		{
			actionRun = new Action("Run") {
				public void run() {
					controller.runCode(false);
				}
			};
			actionRun.setImageDescriptor(ResourceManager.getImageDescriptor(
					MainWindow.class,
					"/de/dhbw/ka/tit09/picsim/ui/icons/run.png"));
			actionRun.setEnabled(false);
		}
		{
			actionWatch = new Action("Watch") {
				public void run() {
					controller.runCode(true);
				}
			};
			actionWatch.setImageDescriptor(ResourceManager.getImageDescriptor(
					MainWindow.class,
					"/de/dhbw/ka/tit09/picsim/ui/icons/watch.png"));
			actionWatch.setEnabled(false);
		}
		{
			actionStop = new Action("Stop") {
				public void run() {
					controller.stopCodeExec();
				}
			};
			actionStop.setImageDescriptor(ResourceManager.getImageDescriptor(
					MainWindow.class,
					"/de/dhbw/ka/tit09/picsim/ui/icons/stop.png"));
			actionStop.setEnabled(false);
		}
		{
			actionPause = new Action("Pause") {
				public void run() {
					controller.pauseCodeExec();
				}
			};
			actionPause.setImageDescriptor(ResourceManager.getImageDescriptor(
					MainWindow.class,
					"/de/dhbw/ka/tit09/picsim/ui/icons/pause.png"));
			actionPause.setEnabled(false);
		}
	}

	/**
	 * Create the menu manager.
	 * 
	 * @return the menu manager
	 */
	@Override
	protected MenuManager createMenuManager() {
		MenuManager menuManager = new MenuManager("menu");

		MenuManager menuManager_1 = new MenuManager("File");
		menuManager.add(menuManager_1);
		menuManager_1.add(actionOpenFile);
		menuManager_1.add(new Separator());
		menuManager_1.add(actionExit);

		MenuManager menuManager_3 = new MenuManager("Run");
		menuManager.add(menuManager_3);
		menuManager_3.add(actionRun);
		menuManager_3.add(actionWatch);
		menuManager_3.add(actionPause);
		menuManager_3.add(actionStop);

		MenuManager menuManager_2 = new MenuManager("Help");
		menuManager.add(menuManager_2);
		menuManager_2.add(actionOpenDocu);
		return menuManager;
	}

	/**
	 * Create the toolbar manager.
	 * 
	 * @return the toolbar manager
	 */
	@Override
	protected ToolBarManager createToolBarManager(int style) {
		ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT | SWT.WRAP);

		ActionContributionItem actionOpenCI = new ActionContributionItem(
				actionOpenFile);
		actionOpenCI.setMode(ActionContributionItem.MODE_FORCE_TEXT);
		toolBarManager.add(actionOpenCI);

		ActionContributionItem actionRunCI = new ActionContributionItem(
				actionRun);
		actionRunCI.setMode(ActionContributionItem.MODE_FORCE_TEXT);
		toolBarManager.add(actionRunCI);

		ActionContributionItem actionWatchCI = new ActionContributionItem(
				actionWatch);
		actionWatchCI.setMode(ActionContributionItem.MODE_FORCE_TEXT);
		toolBarManager.add(actionWatchCI);

		ActionContributionItem actionPauseCI = new ActionContributionItem(
				actionPause);
		actionPauseCI.setMode(ActionContributionItem.MODE_FORCE_TEXT);
		toolBarManager.add(actionPauseCI);

		ActionContributionItem actionStopCI = new ActionContributionItem(
				actionStop);
		actionStopCI.setMode(ActionContributionItem.MODE_FORCE_TEXT);
		toolBarManager.add(actionStopCI);

		return toolBarManager;
	}

	/**
	 * Create the status line manager.
	 * 
	 * @return the status line manager
	 */
	@Override
	protected StatusLineManager createStatusLineManager() {
		// StatusLineManager statusLineManager = new StatusLineManager();
		return null;
	}

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String args[]) {
		Display display = Display.getDefault();
		RealmProvider.setUIRealm(SWTObservables.getRealm(display));
		Realm.runWithDefault(RealmProvider.getUIRealm(), new Runnable() {
			public void run() {
				try {
					MainWindow window = new MainWindow();
					window.setBlockOnOpen(true);
					window.open();
					Display.getCurrent().dispose();
					System.exit(0);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Configure the shell.
	 * 
	 * @param newShell
	 */
	@Override
	protected void configureShell(Shell newShell) {
		newShell.setMinimumSize(new Point(800, 400));
		newShell.setSize(getInitialSize());
		super.configureShell(newShell);
		newShell.setText("PIC Simulator");
	}

	/**
	 * Return the initial size of the window.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(1184, 500);
	}

	/**
	 * Returns the TMR0 text widget.
	 * 
	 * @return the TMR0 text widget
	 */
	public Text getTTMR0() {
		return tTMR0;
	}

	/**
	 * Returns the OPTION text widget.
	 * 
	 * @return the OPTION text widget
	 */
	public Text getTOPTION() {
		return tOPTION;
	}

	/**
	 * Returns the PCL text widget.
	 * 
	 * @return the PCL text widget
	 */
	public Text getTPCL() {
		return tPCL;
	}

	/**
	 * Returns the STATUS text widget.
	 * 
	 * @return the STATUS text widget
	 */
	public Text getTSTATUS() {
		return tSTATUS;
	}

	/**
	 * Returns the FSR text widget.
	 * 
	 * @return the FSR text widget
	 */
	public Text getTFSR() {
		return tFSR;
	}

	/**
	 * Returns the PORTA text widget.
	 * 
	 * @return the PORTA text widget
	 */
	public Text getTPORTA() {
		return tPORTA;
	}

	/**
	 * Returns the PORTB text widget.
	 * 
	 * @return the PORTB text widget
	 */
	public Text getTPORTB() {
		return tPORTB;
	}

	/**
	 * Returns the TRISA text widget.
	 * 
	 * @return the TRISA text widget
	 */
	public Text getTTRISA() {
		return tTRISA;
	}

	/**
	 * Returns the TRISB text widget.
	 * 
	 * @return the TRISB text widget
	 */
	public Text getTTRISB() {
		return tTRISB;
	}

	/**
	 * Returns the EEDATA text widget.
	 * 
	 * @return the EEDATA text widget
	 */
	public Text getTEEDATA() {
		return tEEDATA;
	}

	/**
	 * Returns the EEADR text widget.
	 * 
	 * @return the EEADR text widget
	 */
	public Text getTEEADR() {
		return tEEADR;
	}

	/**
	 * Returns the EECON1 text widget.
	 * 
	 * @return the EECON1 text widget
	 */
	public Text getTEECON1() {
		return tEECON1;
	}

	/**
	 * Returns the PCLATH text widget.
	 * 
	 * @return the PCLATH text widget
	 */
	public Text getTPCLATH() {
		return tPCLATH;
	}

	/**
	 * Returns the INTCON text widget.
	 * 
	 * @return the INTCON text widget
	 */
	public Text getTINTCON() {
		return tINTCON;
	}

	/**
	 * Sets the status of the special function registers text widgets to enabled
	 * or disabled.
	 * 
	 * @param enabled true if widgets enabled
	 */
	public void setRegisterFieldsEnabled(boolean enabled) {
		tTMR0.setEnabled(enabled);
		tOPTION.setEnabled(enabled);
		tPCL.setEnabled(enabled);
		tSTATUS.setEnabled(enabled);
		tFSR.setEnabled(enabled);
		tPORTA.setEnabled(enabled);
		tPORTB.setEnabled(enabled);
		tTRISA.setEnabled(enabled);
		tTRISB.setEnabled(enabled);
		tEEDATA.setEnabled(enabled);
		tEEADR.setEnabled(enabled);
		tEECON1.setEnabled(enabled);
		tPCLATH.setEnabled(enabled);
		tINTCON.setEnabled(enabled);
	}

	/**
	 * Sets the status of the run actions to enabled or disabled.
	 * 
	 * @param enabled true if actions enabled
	 */
	public void setRunActionsEnabled(boolean enabled) {
		actionRun.setEnabled(enabled);
		actionWatch.setEnabled(enabled);
	}

	/**
	 * Sets the status of the stop action to enabled or disabled.
	 * 
	 * @param enabled true if action enabled
	 */
	public void setStopEnabled(boolean enabled) {
		actionStop.setEnabled(enabled);
	}

	/**
	 * Sets the status of the pause action to enabled or disabled.
	 * 
	 * @param enabled true if action enabled
	 */
	public void setPauseEnabled(boolean enabled) {
		actionPause.setEnabled(enabled);
	}

	/**
	 * Sets the status of the open file action to enabled or disabled.
	 * 
	 * @param enabled true if action enabled
	 */
	public void setOpenEnabled(boolean enabled) {
		actionOpenFile.setEnabled(enabled);
	}

	/**
	 * Sets the input of the COM ports drop down list.
	 * 
	 * @param list the list values
	 */
	public void setCOMPorts(String[] list) {
		comboCOMPort.setItems(list);
	}

	/**
	 * Returns the current selected COM port.
	 * 
	 * @return the selected COM port
	 */
	public int getSelectedCOMPort() {
		return comboCOMPort.getSelectionIndex();
	}

	/**
	 * Returns the selection status of the hardware support checkbox.
	 * 
	 * @return true if checkbox is checked
	 */
	public boolean isHardwareEnabled() {
		return btHardwareEnable.getSelection();
	}

	/**
	 * Sets the state of the hardware group.
	 * 
	 * @param enabled true if widgets are enabled
	 */
	public void setHardwareGroupEnabled(boolean enabled) {
		comboCOMPort.setEnabled(enabled);
		btHardwareEnable.setEnabled(enabled);
	}
}
